#define _CRT_SECURE_NO_WARNINGS
#include"Queue.h"

void QueInit(Queue* p)
{
	assert(p);
	p->head = p->tail = NULL;
	p->size = 0;
}

void QuePush(Queue* p, QueDateType x)
{
	assert(p);
	SL* newnode = (SL*)malloc(sizeof(SL));
	if (newnode == NULL)
	{
		perror("QuePush:malloc");
		return;
	}
	newnode->date = x;
	newnode->next = NULL;
	if (p->size == 0)
	{
		p->head = p->tail = newnode;
	}
	else
	{
		p->tail->next = newnode;
		p->tail = newnode;
	}
	p->size++;
}

void QuePop(Queue* p)
{
	assert(p);
	assert(p->head != NULL);
	SL* next = p->head->next;
	free(p->head);
	p->head = NULL;
	p->head = next;
	p->size--;
}


bool QueJuS(Queue* p)
{
	assert(p);
	return p->size == 0;
}

QueDateType QueTop(Queue* p)
{
	assert(p);
	assert(!QueJuS(p));
	return p->head->date;
}

int QueSize(Queue* p)
{
	assert(p);
	return p->size;
}

void QueDestroy(Queue* p)
{
	assert(p);
	SL* cur = p->head;
	while (cur)
	{
		SL* next = cur->next;
		free(cur);
		cur = next;
	}
	p->size = 0;
}

QueDateType QueBack(Queue* p)
{
	assert(p);
	return p->tail->date;
}